﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Moonlite.ExtensionApi.GlobalInterfaces;

namespace Moonlite.ExtensionApi.ControlInterfaces
{
    public abstract class AbstractCustomNode : TreeNode
    {
        private IProjectManager projectManager;
        private Image image;
        public abstract bool CanDragDropToChildren { get; }
        protected IProjectManager ProjectManager
        {
            get
            {
                return projectManager;
            }
        }

        public virtual IProject Project
        {
            get
            {
                TreeNode node = Parent;
                while (node != null)
                {
                    if (node.Tag is IProject)
                    {
                        return (IProject)node.Tag;
                    }

                    node = node.Parent;
                }

                return null;
            }
        }

        public Image Image
        {
            get
            {
                return image;
            }
            set
            {
                if (image != value)
                {
                    image = value;
                    if (image != null)
                    {
                        SetImage(image);
                    }
                }
            }
        }

        protected void SetImage(Image image)
        {
            ExtendedTreeView treeView = (ExtendedTreeView)TreeView;
            if (treeView != null)
            {
                int index = treeView.GetImageIndexForImage(image);
                ImageIndex = SelectedImageIndex = index;
            }
        }

        protected internal virtual void Expanding()
        {
        }

        protected internal virtual void Collapsing()
        {
        }

        public AbstractCustomNode(IProjectManager projectManager)
        {
            if (projectManager == null)
            {
                throw new ArgumentNullException("projectManager");
            }

            this.projectManager = projectManager;
        }
    }
}
